import router from './router'
import NProgress from 'nprogress' 
import 'nprogress/nprogress.css' 
NProgress.configure({ showSpinner: false })
import store from '@/store'
import {  getToken, setLastTabInfo } from "./utils/localStorage";
import { routes } from "@/router/index";
import { getUserByToken } from "@/api/system/user";
import { contentPath } from "@/constant/system.js"

// 路由守卫
router.beforeEach(async(to, from, next) => {
  // 虚拟进度条
  NProgress.start()
  // 设置页面标题
  document.title = '海纳百川有容乃大'
  // 获取本地缓存中的token
  const token = getToken();
  if (token) {
    // 缓存中有token的情况下访问登录页面,直接进入首页(如果访问/,也进入首页)
    if (
      to.path == contentPath + "/login" ||
      to.path == contentPath + "/login/" ||
      to.path == "/" ||
      to.path == contentPath + "" ||
      to.path == contentPath + "/"
    ) {
      next(contentPath + "/home");
      store.commit("addTabList", { path: contentPath + "/home", name: "首页" });
      NProgress.done();
    } else {
      // 已有权限
      const hasPremission =
        store.state.menuList && store.state.menuList.length > 0;
      if (hasPremission) {
        setTabAndCrumbs(to);
        next();
      } else {
        // 根据token取权限信息
        await getUserByToken({ token: token })
          .then((res) => {
            // 用户无权限时,给个权限的空数组
            if (
              !res.data.rights ||
              res.data.rights == undefined ||
              res.data.rights == null
            ) {
              store.commit("setMenuList", []);
            } else {
              store.commit("setMenuList", res.data.rights);
            }
            store.commit("setUserInfo", res.data);
          })
          .catch((error) => {
            // 接口异常处理
            next();
            NProgress.done();
          });
        setTabAndCrumbs(to);
        next();
        NProgress.done();
      }
    }
  } else {
    if (to.path == contentPath + "/login") {
      // 缓存中没有token的情况下访问登录页面,直接进入登录页面
      next();
    } else {
      // 缓存中没有token访问其他页面,直接进入登录页面
      next(contentPath + "/login");
    }
    NProgress.done();
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
// 设置打开的tabList, 同时设置面包屑
function setTabAndCrumbs(to){
  if (to && to.name == "404") {
    return;
  }
  // 刷新时展示选中的路由页面
  store.commit("addTabList", { path: to.path, name: to.name });
  setLastTabInfo({ path: to.fullPath, name: to.name })
  // 添加tab
  const parentName = getMenuNameByPath(to.fullPath);
  if (parentName && to.path != contentPath + "/home") {
    store.commit("setCrumbsName", parentName + " / " + to.name);
  } else {
    store.commit("setCrumbsName", to.name);
  }
}
 // 根据菜单路径获取父菜单名称
function getMenuNameByPath(pathVal){
  let returnName=''
  store.state.menuList.forEach((item) => {
    if (item.path == pathVal) {
      //菜单结构已重组
      returnName = item.name;
    } else if (pathVal.indexOf(item.path) != -1) {
      // 菜单结构未重组, 暂时用这个方法
      returnName = item.rightsName
    } else if (item.children && item.children.length > 0) {
      item.children.forEach((sonItem) => {
        if (sonItem.path == pathVal) {
          returnName = item.name;
        }
      });
    }
  });
  return returnName;
}
